
import FrameBase from "../FrameBase";

export default class LocalStoreage extends FrameBase {
    private static _instance: LocalStoreage = null;
    public static get instance() {
        if (!this._instance) {
            this._instance = new LocalStoreage();
        }
        return this._instance;
    }

    constructor() {
        super();
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            this.platform = globalThis.wx;
        } else if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
            this.platform = globalThis.tt;
        }
    }

    private platform = null;
    public setItem(key: string, value: any) {
        if (cc.sys.platform == cc.sys.DESKTOP_BROWSER || cc.sys.platform == cc.sys.MOBILE_BROWSER) {
            cc.sys.localStorage.setItem(key, JSON.stringify(value));
        } else {
            this.platform.setStorageSync(key, value);
        }
    }

    public getItem(key: any): any {
        if (cc.sys.platform == cc.sys.DESKTOP_BROWSER || cc.sys.platform == cc.sys.MOBILE_BROWSER) {
            return JSON.parse(cc.sys.localStorage.getItem(key));
        } else {
            return this.platform.getStorageSync(key);
        }
    }

}